package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dao.SysDeptDao;
import com.itheima.dao.SysUserDao;
import com.itheima.dao.TbAssignRecordDao;
import com.itheima.dto.AssignRecordDto;
import com.itheima.pojo.SysDept;
import com.itheima.pojo.SysUser;
import com.itheima.pojo.TbAssignRecord;
import com.itheima.service.SysDeptService;
import com.itheima.service.TbAssignRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (TbAssignRecord)表服务实现类
 *
 * @author makejava
 * @since 2022-01-16 15:48:53
 */
@Slf4j
@Service("tbAssignRecordService")
public class TbAssignRecordServiceImpl implements TbAssignRecordService {
    @Resource
    private TbAssignRecordDao tbAssignRecordDao;
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private SysDeptDao sysDeptDao;


    @Override
    public R addAssignRecord(Long clueId, TbAssignRecord tbAssignRecord) {
        tbAssignRecord.setAssignId(clueId);
        int insert = tbAssignRecordDao.insert(tbAssignRecord);
        if (insert >0) {
            return new R(1,"分配添加成功!",null);
        }
        return new R(0,"分配添加失败!",null);
    }

    @Override
    public R page(AssignRecordDto assignRecordDto) {
        if (assignRecordDto.getPage()==null) {
            return R.error("请求页数为空");
        }
        IPage<TbAssignRecord> pageInfo = new Page<>(assignRecordDto.getPage(),assignRecordDto.getPageSize());
        //6个搜索条件
        // 1.AssId
        // 2.userId
        // 3.userName
        // 4.status
        // 5.type
        // 6.startTime endTime
        LambdaQueryWrapper<TbAssignRecord> lqw=new LambdaQueryWrapper<>();
        lqw.like(assignRecordDto.getId()!=null,TbAssignRecord::getId,assignRecordDto.getId());
        lqw.like(assignRecordDto.getUserId()!=null,TbAssignRecord::getUserId,assignRecordDto.getUserId());
        lqw.like(assignRecordDto.getUserName()!=null,TbAssignRecord::getUserName,assignRecordDto.getUserName());
        lqw.like(assignRecordDto.getStatus()!=null,TbAssignRecord::getStatus,assignRecordDto.getStatus());
        lqw.like(assignRecordDto.getType()!=null,TbAssignRecord::getType,assignRecordDto.getType());
        lqw.between(assignRecordDto.getStartTime()!=null&&
                        assignRecordDto.getEndTime()!=null,
                        TbAssignRecord::getCreateTime,
                        assignRecordDto.getStartTime(),
                        assignRecordDto.getEndTime()
                );
        tbAssignRecordDao.selectPage(pageInfo,lqw);
        return R.success("转派记录分页展示",pageInfo);
    }

    @Override
    public R getById(Long id) {
        //2.转派详情
        // 2.1回显具体类型:前端判断
        // 2.2详情:计数 相关用户的 相关类型单子计数
        TbAssignRecord tbAssignRecord = tbAssignRecordDao.selectById(id);
        Long userId = tbAssignRecord.getUserId();
        String type = tbAssignRecord.getType();
        List<TbAssignRecord> tbAssignRecords = tbAssignRecordDao.selectListByUserIdAndType(userId, type);
        Integer typeCount = tbAssignRecords.size();
        // 2.3部门:查部门表
        if (tbAssignRecord.getDeptId() != null) {
            SysDept sysDept =sysDeptDao.selectByUserId(tbAssignRecord.getDeptId());
            tbAssignRecord.setDept(sysDept.getDeptName());
        }
        tbAssignRecord.setCreateTime(new Date());
        tbAssignRecord.setCreateBy("当前登录用户");
        //tbAssignRecord.setNextTime(new Date()+8天);
        Map map=new HashMap();
        map.put("typeCount",typeCount);
        return new R(1,"转派详情",tbAssignRecord,map);
    }

    @Override
    public R updateList(List<TbAssignRecord> assignList, Long userId) {
        SysUser sysUser = sysUserDao.selectById(userId);
        assignList.forEach(assign->{
            assign.setUserId(userId);
            assign.setUserName(sysUser.getUserName());
            assign.setDeptId(sysUser.getDeptId());
            assign.setCreateTime(new Date());
            assign.setCreateBy("当前登录用户");
            //assign.setNextTime(new Date()+8天);
            int i = tbAssignRecordDao.updateById(assign);
            if (i >0) {
                log.info("批量转派成功:{}",i);
            }
        });
        return R.success("批量转派成功",null);
    }

}
